package com.dc.gameserver.serverCore.controller.handleController.itemsController;

import com.dc.gameserver.proto.itemserverprotoVo.bookLoadingRequest;
import com.dc.gameserver.proto.itemserverprotoVo.bookLoadingResponse;
import com.dc.gameserver.proto.itemserverprotoVo.bookVO;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRoleBook;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:13-12-27 </br>
 *         Time:下午2:34 </br>
 *         Package:{@link com.dc.gameserver.serverCore.controller.handleController.itemsController}</br>
 *         Comment：秘籍加载请求
 */
@Service
public class bookLoadingController extends AbstractController {


    @Resource
    private PlayerService playerService;


    @Override
    public void PostConstruct() {
        int ID= bookLoadingRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=bookLoadingRequest.getDefaultInstance();
    }

    /**
     * messageLite数据结构体分发
     *
     * @param messageLite 数据载体
     * @param player      active object
     * @throws Exception 注意messageLite应该递交给GC直接处理  ，set null
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        bookLoadingRequest loadingRequest= (bookLoadingRequest) messageLite;
        int type=loadingRequest.getType();   //部位.类型
        int data=loadingRequest.getClientdata();
        loadingRequest=null;
        FastTable<TRoleBook> books=playerService.bookLoading(player.getID(),type);
        bookLoadingResponse.Builder builder=bookLoadingResponse.newBuilder();
        if (books!=null){
            for (TRoleBook book:books){
                bookVO.Builder bookVo=bookVO.newBuilder();
                bookVo.setBookID(book.getBookid());
                bookVo.setBookTypeID(book.getBooktypeid());
                bookVo.setBookSuccessUseTimes(book.getBooksuccessusetimes());
                bookVo.setBookTimeOut(book.getBooktimeout());
                bookVo.setBookLocation(book.getBooklocation());
                bookVo.setBookPinZhi(book.getBookpinzhi());
                bookVo.setBookPinJie(book.getBookpinjie());
                bookVo.setBookPinJieExp(book.getBookpinjieexp());
                bookVo.setItemType(book.getItemtype());
                builder.addBookList(bookVo.build());
            }
            books.clear();
            books=null;
        }
        builder.setType(type);
        builder.setClientdata(data); //client data
        player.DirectSend(wrappedBufferShort(bookLoadingResponse.msgID.ID_VALUE,builder.build()));
    }
}
